package com.dms.controller;

import com.dms.pojo.Admin;
import com.dms.pojo.User;
import com.dms.service.AdminManageService;
import com.dms.utils.JwtToken;
import com.dms.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: AdminManageController
 * @package: com.dms.controller
 * @description  对管理员模块的操作
 * @author: sk
 */
@Controller
@RequestMapping("/adminManageController")
public class AdminManageController {

    @Autowired
    AdminManageService userService;


    /**
     *  登录
     * @return
     * @throws Exception
     */
    @PostMapping("/userLogin")
    @ResponseBody
    /*@RequestParam(value = "userName",required = false) String userName , @RequestParam(value = "password",required = false) String password,
    @RequestParam(value = "userType")Integer userType*/
    public Msg userLogin(@RequestBody Map<String, Object> map) throws Exception {

        //调用登录业务逻辑
        User user = userService.userLogin(map.get("userName").toString(), map.get("password").toString(), (Integer)map.get("userType"));
        System.out.println(user);
        if (user == null)
            return Msg.fail();
        else{
            //账号被禁用
            if (user.getAdmin().getAdminId() != null){
                if (!user.getAdmin().isAdminStatus())
                    return Msg.exception();
            }
            try {
                //如果类型不匹配，登录失败
                if (!user.getUserType().equals((Integer)map.get("userType"))){
                    return Msg.fail();
                }
                if (user != null) {
                    //登录成功 分发token令牌
                    return Msg.success().add("token", JwtToken.createToken(map.get("userName").toString(), map.get("password").toString())).add("user",user);
                } else {
                    return Msg.fail();
                }
            }catch (Exception e){
                return Msg.fail();
            }
        }
    }

    /**
     * 分页模糊查询管理员列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findUserList")
    @ResponseBody
    public Msg findUserByLimit(@RequestParam("query") String query, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
        List<Admin> adminList = userService.findUserByLimit(query, pageNum, pageSize);
        return Msg.success().add("userList",adminList).add("total",userService.selectCount(query));
    }


    /**
     * 修改用户 status 状态
     * @return
     */
    @PutMapping("/updateUserStatus")
    @ResponseBody
    public Msg updateUserStatus(@RequestBody Admin admin){
        int num = userService.updateUserStatus(admin.getAdminId(), admin.isAdminStatus());
        if (num > 0)
            return Msg.success();
        else
            return Msg.fail();
    }

    /**
     * 用户名是否已存在验证
     * @param userName
     * @return
     */
    @GetMapping("/checkUserName")
    @ResponseBody
    public Msg checkUserName(@RequestParam("userName") String userName){
        Admin admin = userService.checkUserName(userName);
        if (admin == null)
           return Msg.fail();
        return Msg.success();
    }

    /**
     * 添加新的管理员用户
     * @param admin
     */
    @PostMapping("/addAdmin")
    @ResponseBody
    public Msg addAdmin(@RequestBody Admin admin){
        int num = userService.addAdmin(admin);
        if (num > 1) {
            return Msg.success();
        }else {
            return Msg.fail();
        }
    }

    /**
     * 根据id查找admin用户
     * @param adminId
     * @return
     */
    @GetMapping("/getAdminInfo")
    @ResponseBody
    public Msg getAdminInfo(@RequestParam("id") int adminId){
        return Msg.success().add("adminInfo",userService.getAdminInfo(adminId)).add("buildingList", userService.getBuildingList());
    }

    /**
     * 修改管理员用户信息
     * @param admin
     * @return
     */
    @PutMapping("/editAdminInfo")
    @ResponseBody
    public Msg editAdminInfo(@RequestBody Admin admin){
        System.out.println(admin);
        int num = userService.editAdminInfo(admin);
        if (num > 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    /**
     * 删除管理员用户信息
     * @param userName
     * @return
     */
    @DeleteMapping("/deleteAdminInfo")
    @ResponseBody
    public Msg deleteAdminInfo(@RequestParam("userName") String userName){
       int num = userService.deleteAdminInfo(userName);
        if (num > 0) {
            return Msg.success();
        }
        return Msg.fail();
    }

    /****************************************************************************************************/

    /**
     * 获取角色权限列表
     * @return
     */
    @GetMapping("/getAuthorityList")
    @ResponseBody
    public Msg getAuthorityList(@RequestParam("userType") int userType){
        return Msg.success().add("authorityList", userService.getAuthorityList(userType));
    }

    /**
     * 修改角色权限
     * @param map
     * @return
     */
    @PutMapping("/updateAuthority")
    @ResponseBody
    public Msg updateAuthority(@RequestBody HashMap<String,Object> map){
        String arr = (String) map.get("arr");
        String noSelectArr = (String) map.get("noSelectArr");
        return userService.updateAuthority(arr,noSelectArr) >= 0 ? Msg.success() : Msg.fail();
    }

}
